<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: CrazyTobis
  Date: 2020-05-11
  Time: 14:08
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<div class="container" data-tcj="tcjStore" style="max-width: 90%">
        <!-- row1 - 新增按钮和删除按钮 -->
        <div class="row" style="margin: 15px 0 0 0;">
            <div class="col-5 row">
                <button class="col-2 btn btn-outline-primary " data-tcj="rk-add" type="button" data-toggle="" data-target="">
                    新增
                </button>
                <button class="col-2 btn btn-outline-danger" data-tcj="rk-delete" type="button" data-toggle="" data-target="" style="margin-left: 20px;">
                    删除
                </button>
                <button class="col-2 btn btn-outline-warning" data-tcj="rk-tijiao" type="button" data-toggle="" data-target="" style="margin-left: 20px;">
                    提交
                </button>
                <button class="col-2 btn btn-outline-info" data-tcj="rk-shenhe" type="button" data-toggle="" data-target="" style="margin-left: 20px;">
                    审核
                </button>
            </div>
        </div>

        <!-- row-2 - 查询信息的代码块 -->
        <form>
            <div class="row" style="margin-top: 5px ;">
                <div class="col-12 row">
                    <!-- 请输入单号输入框 -->
                    <div class="input-group col-2">
                        <input class="form-control" type="text" name="receptNo" placeholder="请输入入库单号" />
                    </div>
                    <!-- 关联单号输入框 -->
                    <div class="input-group col-2">
                        <input class="form-control" type="text" name="orderNo" placeholder="请输入关联单号" />
                    </div>
                    <%--入库类型--%>

                    <div class="col-2">
                    <select class="form-control" name="stateId">
                        <option value="" hidden>请选择单据状态</option>
                        <c:forEach items="${name}" var="obj">
                        <option value="${obj.id}">${obj.billStateName}</option>
                        </c:forEach>
                    </select>
                    </div>
                    <%--单据状态类型--%>
                    <div class="col-2">
                    <select class="form-control" name="typeId">

                        <option value="" hidden>请选择单据类型</option>
                        <c:forEach items="${result}" var="obj">
                        <option value="${obj.id}">${obj.name}</option>
                        </c:forEach>
                    </select>
                    </div>

                    <%--制单人--%>
                    <div class="col-2">
                    <select class="form-control" name="prepared">
                        <option value="">制单人</option>
                        <option value="张三">张三</option>
                    </select>
                    </div>
                    <div class="col-2 ">
                        <button class=" btn btn-outline-primary " data-tcj="select" type="button" data-toggle="" data-target="" style="height: 37px;">
                            查询
                        </button>
                        <button class=" btn btn-outline-danger"  type="reset" data-toggle="" data-target="" style="margin-left: 20px; height: 37px;">
                            重置
                        </button>
                    </div>
                </div>

            </div>



                    <!-- row3 - 日期和查询重置按钮的代码块-->
            <div class="row " style="margin-top: 5px ;">
                <div class="form-group col-3">
                    <div class="input-group">
                        <div class="input-group-prepend">
					<span class="input-group-text">
					        <span class="fa fa-calendar-o"></span>
					</span>
                            <!-- 开始日期和结束日期输入框 -->
                        </div>
                        <input type='text' name="startTime" class="form-control pickadate" placeholder="开始日期" style="background-color: white"/>
                        <span class="input-group-text" style="background: white;">-</span>
                        <input  name="endTime" type='text' class="form-control pickadate" placeholder="结束日期"  style="background-color: white"/>
                    </div>
                </div>

                <div class="col-5">

                </div>


            </div>


        </form>

        <div id="myTable">

        </div>
</div>



<script>
    (function () {

        //用来储存选中的订单
        var checkedNode = [];



        $.post("store.html","",function (e) {
            let pageVO = e.data;
            let columns = ["ID","入库单号","单据状态","入库类型","创建时间","备注","关联单号"];
            columns = getColumn(e.data.data,columns);
            columns = skip(columns,"ID");
            let options = {
                node:"#myTable",
                pageVO:e.data,
                columns:columns,
                isEdit:true,
                    fontAwesome:{'ft-edit':'编辑' , 'ft-check-square' : '审核', 'ft-log-out' : '确认入库', 'ft-eye':'详情', 'ft-trash-2' : '删除'},
                fontAwesomeClick:{"编辑":function (event,obj,node,parent) {
                        $.post("edit",obj,function (e) {

                            if(e == "true"){
                                $('[data-tcj="tcjStore"]').addMianbao('edit',"入库单修改",obj);
                            }else {
                                alert("当前单号状态不能编辑!");
                            }
                        });
                    },
                    "详情":function (event,obj,node,parent) {
                        $("").addMianbao("tcjDetail","入库单详情",obj);

                    },
                    "审核":function (event,obj,node,parent) {
                                if(obj.result!='待审核'){
                                    alert("该订单目前不能进行审核操作!");
                                    return;
                                }
                            swal({
                                title: '是否通过审核? ',
                                text: "(点击空白处返回)",
                                type: 'warning',
                                showCancelButton: true,
                                confirmButtonColor: '#0CC27E',
                                cancelButtonColor: '#FF586B',
                                confirmButtonText: '审核通过',
                                cancelButtonText: '审核拒绝',
                                confirmButtonClass: 'btn btn-success btn-raised mr-5',
                                cancelButtonClass: 'btn btn-danger btn-raised',
                                buttonsStyling: false
                            }).then(function () {
                                swal(
                                    '通过审核',
                                    '',
                                    'success'
                                );
                                //调用后端将通过审核
                                let order = [obj.receptNo];
                                //将单据状态改变
                                $.ajax({
                                    url:'submit',
                                    type:'post',
                                    dataType: 'json',
                                    traditional:true,
                                    data:{'审核':order},
                                    success:function (e) {
                                        if(e>0){

                                            //刷新页面
                                            $.post("store.html",function (e) {

                                                let newOptions = $.options;
                                                newOptions.pageVO = e.data;
                                                firstTable(newOptions);
                                            },'json');

                                        }else{
                                            alert("操作失败");

                                        }
                                    }
                                });

                            }, function (dismiss) {
                                // dismiss can be 'overlay', 'cancel', 'close', 'esc', 'timer'
                                if (dismiss === 'cancel') {
                                    swal(
                                        '审核拒绝',
                                        '',
                                        'error'
                                    );
                                    $.post('deny',{"data":obj.receptNo},function (e) {
                                        if(e>0){
                                            //刷新页面
                                            $.post("store.html",function (e) {

                                                let newOptions = $.options;
                                                newOptions.pageVO = e.data;
                                                firstTable(newOptions);
                                            },'json');
                                        }else {
                                            alert("操作失败");
                                        }
                                    },'json');
                                }
                            })

                    },
                    "确认入库":function(event,obj,node,parent){
                        if(obj.result!='待确认'){
                            alert("该订单目前不能进行该操作!");
                            return;
                        }
                        //删除模态

                        if($('[data-tcj="mod"]').length != 0){
                            $('[data-tcj="mod"]').remove();
                        }
                        //调用确认入库的页面
                        $('').addMianbao('requestPut',"确认入库",obj);
            },
                    "删除" : function (event,obj,node,parent) {
                        if(obj.result!='待提交'){
                            alert("该订单目前不能进行该操作!");
                            return;
                        }

                        swal({
                            title: '是否确认要删除该订单?',
                            text: "",
                            type: 'warning',
                            showCancelButton: true,
                            confirmButtonColor: '#0CC27E',
                            cancelButtonColor: '#FF586B',
                            confirmButtonText: '删除',
                            cancelButtonText: "取消"
                        }).then(function (isConfirm) {
                            //删除订单
                            if (isConfirm) {
                            $.get("deny",obj,function (e) {
                                    if(e>0){

                                            swal(
                                                '删除成功!',
                                                '',
                                                'success'
                                            );
                                        //刷新页面
                                        $.post("store.html",function (e) {

                                            let newOptions = $.options;
                                            newOptions.pageVO = e.data;
                                            firstTable(newOptions);
                                        },'json');
                                    }else {
                                        swal(
                                            '删除失败!',
                                            '未知错误！',
                                            'success'
                                        );
                                    }
                              },"json");
                            }

                        }).catch(swal.noop);



                    }
                },
                click:function (clickNode,cloumns,tempDate) {
                    if(clickNode.nodeName !=="TR"){
                        return;
                    }

                    //得到对象
                    let obj = getObj(clickNode,cloumns,true,true,true);

                    //判断点击的是否被选中
                    let input = $(clickNode).find('input')[0].checked;
                    if(input){
                        //如果选中则将对象加进去
                        if(JSON.stringify(checkedNode).indexOf(JSON.stringify(obj)) == -1){
                            checkedNode.push(obj);
                        }
                    }else {

                        //循环遍历临时数组如果有则删除

                        for (let i in checkedNode){
                            let o = checkedNode[i];

                            if(obj.receptNo == o.receptNo){
                                checkedNode.splice(i,1);
                                break;
                            }
                        }
                    }


                },
                callBack:function (e,node) {
                    //调用deletI
                    deleteI(e,node);
                }
            }
            firstTable(options);
            $.options = options;
        },"json");
        
        let string = getCalendar();
        eval(string);

        $('[data-tcj="select" ] ').on("click",function (event) {
            event.preventDefault();
            let data = $("form").serialize();
            $.ajax({
                type:"post",
                data:data,
                dataType:"JSON",
                url:"store.html",
                success:function (e) {
                    let newOptions = $.options;
                    newOptions.pageVO = e.data;
                    firstTable(newOptions);
                }
            })
        });

        //新增监听
        $('[data-tcj="rk-add"]').on("click",function (e) {

            $("").addMianbao("addRecept","新增入库单");
        });
        //提交监听
        $('[data-tcj="rk-tijiao"]').on('click',function () {
            //判断选中的临时数组是否有数据
            if(checkedNode.length == 0){
                alert("请选中至少一个订单！");
            }else {
                //循环遍历临时数组
                //用来储存筛选后的提交的订单
                let sendSbill = [];
                for (let o of checkedNode){
                    if(o.result == '待提交'){
                        sendSbill.push(o.receptNo);
                    }

                }

                //拼接提示框的信息
                let str = "是否确定提交选中的订单:";
                for (let o of sendSbill){

                    str+= '['+o+']';
                }
               let success =  confirm(str);

                if(success){
                    //判断最后是否有待提交的订单
                    if(sendSbill.length == 0){
                        alert("请选择待提交的订单！");
                        return;
                    }

                    //将单据状态改变
                    $.ajax({
                        url:'submit',
                        type:'post',
                        dataType: 'json',
                        traditional:true,
                        data:{'提交':sendSbill},
                        success:function (e) {
                           if(e>0){
                               alert("提交成功");
                               //刷新页面
                               $.post("store.html",function (e) {

                                   let newOptions = $.options;
                                   newOptions.pageVO = e.data;
                                   firstTable(newOptions);
                               },'json');
                               sendSbill = [];
                               checkedNode = [];

                           }else{
                               alert("提交失败");
                               sendSbill = [];
                               checkedNode = [];
                           }
                        }
                    })
                }
            }
        });


        //审核监听
        $('[data-tcj="rk-shenhe"]').on('click',function (e) {
            //判断选中的临时数组是否有数据
            //判断选中的临时数组是否有数据
            if(checkedNode.length == 0){

                alert("请选中至少一个订单！");
            }else {
                //循环遍历临时数组
                //用来储存筛选后的提交的订单
                let sendSbill = [];
                for (let o of checkedNode) {
                    if (o.result == '待审核') {
                        sendSbill.push(o.receptNo);
                    }

                }

                //拼接提示框的信息
                let str = "是否确定审核选中的订单:";
                for (let o of sendSbill) {

                    str += '[' + o + ']';
                }
                let success = confirm(str);

                if (success) {
                    //判断最后是否有待提交的订单
                    if (sendSbill.length == 0) {
                        alert("请选择待审核的订单！");
                        return;
                    }

                    //将单据状态改变
                    $.ajax({
                        url:'submit',
                        type:'post',
                        dataType: 'json',
                        traditional:true,
                        data:{'审核':sendSbill},
                        success:function (e) {
                            if(e>0){
                                alert("审核成功");
                                //刷新页面
                                $.post("store.html",function (e) {

                                    let newOptions = $.options;
                                    newOptions.pageVO = e.data;
                                    firstTable(newOptions);
                                },'json');
                                sendSbill = [];
                                checkedNode = [];

                            }else{
                                alert("审核失败");
                                sendSbill = [];
                                checkedNode = [];
                            }
                        }
                    });
                }
            }
        });

        /**
         * 用来清除表格每行尾部的节点图标，符合 array 数组里的名称保留，node 为 tr
         */
        function removeTitleIcon(node,array) {
            $(node).find('td a[title]').each(function () {
                let title = $(this).attr('title');
                let boo = false;

                for (let temp of array){
                    if(temp === title){
                        // return false;
                        boo = true;
                        break;
                    }
                }

                if(!boo){
                    this.remove();
                }
            });
        }
        
        function deleteI(options,node) {
                node.find('tbody').find('tr').each(function (index,ele) {
                   //查找到tr的审核状态
                    let obj = getObj(ele,options.columns);
                    let stats = obj.result;
                    switch (stats) {
                        case "待提交":
                            removeTitleIcon(ele,["编辑","详情","删除"]);
                            break;
                        case "待审核":
                            removeTitleIcon(ele,["详情","审核"]);
                            break;
                        case "待确认":
                            removeTitleIcon(ele,["详情","确认入库"]);
                            break;
                        case "审核拒绝":
                            removeTitleIcon(ele,["详情"]);
                            break;
                        case"已完成":
                            removeTitleIcon(ele,["详情"]);
                    }
                })
        }


        //监听返回的监听
        $(getShowMianBao()).on('click',function (e) {
            if( $('[ data-tcj="mod"]').length != 0){
                $('[ data-tcj="mod"]').remove();
            }

            if($('[data-tcj="addRkMode"]').length != 0){
                $('[data-tcj="addRkMode"]').remove();
            }
        });

    })();
</script>